package com.taptech.doufu.ui.view.personalcenter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taptech.doufu.bean.EventBean;
import com.taptech.doufu.util.TextUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.MainHomeActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.util.TagsUtil;
import com.taptech.doufu.ui.view.topicview.UmengEventUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by fishboneLsy on 2015/8/11.
 */
public class ContentTagsViewGroup extends ViewGroup implements View.OnClickListener
{

    Context mContext;

    private int tagsWidthSpace;
    private int tagsHeightSpace;

    private int tagsBackgroundResource = 0;
    private int tagsSetBackgroundResource = 0;
    private int tagsTextColor = Color.parseColor("#ff8e8e8e");
    private int tagsTextSize = 13;

    private int tagsTextLightColor = Color.parseColor("#ff6e70");

    private int type = -1;
    private String sexType = "";

    private int maxRows = Integer.MAX_VALUE;


    public ContentTagsViewGroup(Context context)
    {

        super(context);
        initView(context);
    }

    public ContentTagsViewGroup(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        initView(context);
    }

    public ContentTagsViewGroup(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        initView(context);
    }


    private void initView(Context context)
    {
        mContext = context;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        View childView;
        int childWidth = 0;
        int childHeight = 0;
        int row = 0;
        int num = 0;
        int parentWidthLeave = this.getMeasuredWidth();
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++)
        {
            childView = getChildAt(i);
            childWidth = childView.getMeasuredWidth() + tagsWidthSpace;
            childHeight = childView.getMeasuredHeight() + tagsHeightSpace;
            //此处只计算控件是否能放下，忽略右边的空格
            if (childWidth - tagsWidthSpace > parentWidthLeave)
            {
                row++;
                num = 0;
                parentWidthLeave = this.getMeasuredWidth();
            }
            if (row < maxRows && maxRows > 0)
            {
                measureChild(childView, MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                num++;
                parentWidthLeave -= childWidth;
            }

        }
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, ((row < maxRows ? row : maxRows) + 1) * childHeight - tagsHeightSpace);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        if (true)
        {
            View childView;
            int childWidth = 0;
            int childHeight = 0;
            int row = 0;
            int num = 0;
            int parentWidthLeave = this.getMeasuredWidth();
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++)
            {
                childView = getChildAt(i);
                childWidth = childView.getMeasuredWidth() + tagsWidthSpace;
                childHeight = childView.getMeasuredHeight() + tagsHeightSpace;
                // 此处只计算控件是否能放下，忽略右边的空格
                if (childWidth - tagsWidthSpace > parentWidthLeave)
                {
                    row++;
                    num = 0;
                    parentWidthLeave = this.getMeasuredWidth();
                }

                if (row < maxRows && maxRows > 0)
                {
                    childView.layout(this.getMeasuredWidth() - parentWidthLeave, childHeight * row, this.getMeasuredWidth() - parentWidthLeave + childWidth - tagsWidthSpace, childHeight * (row + 1) - tagsHeightSpace);
                    num++;
                    parentWidthLeave -= childWidth;
                } else
                {

                }


            }
        }
    }

    public void addTextTags(Context context, String[] tagStrings)
    {
        for (String tagStr : tagStrings)
        {
            addTextTag(context, tagStr);
        }
    }

    public void setTextTags(Context context, String[] tagStrings)
    {
        this.removeAllViews();
        addTextTags(context, tagStrings);
    }

    public void addTextTag(Context context, String tagString)
    {
        if (tagString != null && !TextUtils.isEmpty(tagString))
        {
            TextView textView = new TextView(context);
            textView.setPadding(0, 0, 0, 0);
            textView.setSingleLine(true);
            //textView.setMaxEms(8); //限制最大长度
            textView.setEllipsize(TextUtils.TruncateAt.END);
            textView.setIncludeFontPadding(false);
            textView.setTag(tagString);
           // tagString = TextUtil.subString(tagString, 9);
            textView.setText(tagString);
            handleTextViewStyle(textView);
            addView(textView);
        }
    }

    public void lightChildTextView(String tagString)
    {
        if (tagString != null && !TextUtil.isEmpty(tagString))
        {
            for (int i = 0; i < this.getChildCount(); i++)
            {
                TextView textView = (TextView) this.getChildAt(i);
                if (tagString.equals(textView.getText().toString()))
                {
                    textView.setTextColor(tagsTextLightColor);
                } else
                {
                    textView.setTextColor(tagsTextColor);
                }
            }
        }


    }

    public void addTextTag(TextView textView)
    {
        if (textView != null && !TextUtils.isEmpty(textView.getText()))
        {
            addView(textView);
        }
    }


    private void handleTextViewStyle(TextView textView)
    {
        if (tagsBackgroundResource == 0)
        {
            tagsBackgroundResource = R.drawable.tag_background_draw;
        }
        tagsWidthSpace = ScreenUtil.dip2px(mContext, 7);
        tagsHeightSpace = ScreenUtil.dip2px(mContext, 9);

        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            tagsBackgroundResource = R.drawable.tag_background_draw_dark;
            tagsTextColor = mContext.getResources().getColor(R.color.text_dark);
        }else {
            tagsTextColor = Color.parseColor("#ff8e8e8e");
        }

        if (tagsBackgroundResource != 0)
        {
            textView.setBackgroundResource(tagsBackgroundResource);
        }
        if (tagsTextColor != 0)
        {
            textView.setTextColor(tagsTextColor);
        }
        if (tagsTextSize > 0)
        {
            textView.setTextSize(tagsTextSize);
        }
        int paddingLeft = ScreenUtil.dip2px(mContext, 11);
        int paddingTop = ScreenUtil.dip2px(mContext, 3);
        int paddingRight = ScreenUtil.dip2px(mContext, 11);
        int paddingBottom = ScreenUtil.dip2px(mContext, 3);
        textView.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
        textView.setIncludeFontPadding(false);
        textView.setOnClickListener(this);

    }

    public void setTagsWidthSpace(int width)
    {
        tagsWidthSpace = width;
    }

    public void setTagsHeightSpace(int height)
    {
        tagsHeightSpace = height;
    }

    public void setTagsBackGroundResource(int backgroundId)
    {
        tagsBackgroundResource = backgroundId;
        tagsSetBackgroundResource = backgroundId;
    }

    public void setTagsTextColor(int textColor)
    {
        tagsTextColor = textColor;
    }

    public void setTagsTextSize(int textSize)
    {
        tagsTextSize = textSize;
    }

    @Override
    public void onClick(View view)
    {
        UmengEventUtil.addContentEvent(mContext,eventBean,sexType);
        if (view.getTag() != null && !TextUtil.isEmpty(view.getTag().toString()))
        {
            if (type == -1)
            {
                TagsUtil.openWeexTagActivity(mContext,view.getTag().toString(),18,sexType);
//                TagsUtil.enterTagSearchActivity(mContext, view.getTag().toString());
            } else if (type == Constant.OBJECT_TYPE_CARTOON) {
                return;
            } else {
                TagsUtil.openWeexTagActivity(mContext,view.getTag().toString(),type,sexType);

//                TagsUtil.enterTagSearchActivity(mContext, view.getTag().toString(), type + "");
            }
        }
        try
        {
            Activity activity = (Activity) mContext;
            Map<String, String> analysisMap = new HashMap<String, String>();
            if (view != null && view.getTag() != null)
            {
                analysisMap.put("tagName", view.getTag().toString());
            }
            if (activity instanceof MainHomeActivity)
            {
                TMAnalysis.event(mContext, "home-tag", analysisMap);
            }
        } catch (Exception e)
        {   //说明是ApplicationContext
            e.printStackTrace();
        }
    }


    public int getMaxRows()
    {
        return maxRows;
    }

    public void setMaxRows(int maxRows)
    {
        this.maxRows = maxRows;
    }

    private EventBean eventBean;
    public void setEventData(EventBean eventData) {
        this.eventBean = eventData;
    }


    public void setType(int type)
    {
        this.type = type;
    }

    public void setSexType(String sexType) {
        this.sexType = sexType;
    }

    public void changeViewBg() {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View childView = getChildAt(i);
            if (childView != null && childView instanceof TextView) {
                TextView textView = (TextView) childView;
                if(WeMediaApplication.getInstance().isDayNightMode_Night){
                    textView.setBackground(getResources().getDrawable(R.drawable.biankuang_home_novel_tag_dark));
                }else {
                    textView.setBackground(getResources().getDrawable(R.drawable.tag_background_draw));
                }
            }
        }
    }
}